package com.sghy.xcxservice.controller.project;

import com.sghy.xcxservice.common.CustomException;
import com.sghy.xcxservice.common.Error;
import com.sghy.xcxservice.controller.BaseController;
import com.sghy.xcxservice.dto.RoomListVo;
import com.sghy.xcxservice.dto.UserInfoVo;
import com.sghy.xcxservice.entity.BgbRoom;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.sghy.xcxservice.common.TokenInterceptor.getUid;

@RequestMapping("/project")
@RestController
public class projectController extends BaseController {

    /**
     * 创建项目，通过名称和项目编号
     * @param projectName
     * @param projectNumber
     * @return
     */
    @PostMapping("createProject")
    public Map<String, Object> createProject(@RequestParam String projectName, @RequestParam String projectNumber) {
        //在roomService中创建方法，projectNumber是唯一的，不可重复
        if (roomService.isProjectNumberExists(projectNumber)) {
            throw new CustomException(Error.PARAM_ERROR, "项目编号已存在，请使用其他编号");
        }
        roomService.createProject(projectName, projectNumber);
        return SUS("项目创建成功");
    }
    /**
     * 按照分页查询项目列表
     */
    @PostMapping("getProjectList")
    public Map<String, Object> getProjectList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue
            = "20")  int size,String like) {
        // 在roomService中创建方法，返回分页数据
        return SUS(roomService.getProjectList(page, size, like));
    }

    /**
     * 获取项目详情
     */
    @PostMapping("getProjectInfo")
    public Map<String, Object> getProjectInfo(String roomNo) {
        RoomListVo info = roomService.getProjectInfo(roomNo);
        return SUS(info);
    }

    /**
     * 删除内容
     * @param contentId
     * @return
     */
    @PostMapping("delContent")
    public Map<String, Object> delContent(String contentId) {
        roomContentService.delContent(contentId);
        return SUS("删除成功！");
    }
    
    
    
    /**
     * 发布一条动态
      * @param uid
      * @param content
     * @param files
     */
    @PostMapping("publishDynamic")
    public Map<String, Object> publishDynamic(@RequestParam String uid,@RequestParam String roomNo,
                                              @RequestParam String content,String contentId,
                                              @RequestParam(required = false) String files) {
        BgbRoom room = roomService.getByRoomNo(roomNo);
        if (room==null){
            throw new CustomException(Error.PARAM_ERROR, "项目不存在");
        }
        if (contentId == null || contentId.isEmpty()) {
            roomContentService.publishDynamic(uid,roomNo, content, files);
        }else {
            // 调用发布动态的服务
            roomContentService.publishDynamic(contentId,getUid(),roomNo, content, files);
        }
        return SUS("动态发布成功");
    }

    /**
     * 查询项目中的内容列表
     */
    @PostMapping("getProjectContentList")
    public Map<String, Object> getProjectContentList(@RequestParam String roomNo, @RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "20") int size) {
        // 调用roomContentService查询项目中的内容列表
        return SUS(roomContentService.getProjectContentList(roomNo, page, size));
    }





}
